import Vue from 'vue'
import VueResource from 'vue-resource'
Vue.use(VueResource)
	// import './debug.js'

let loading = false;
let watcher = {};

Vue.http.interceptors.push((request, next) => {
	if (loading) {
		//防止重复提交
		if (watcher.url == request.url && JSON.stringify(watcher.body) == JSON.stringify(request.body)) {
			return false;
		}
	}
	loading = true; //是否进入重复提交验证
	watcher.url = request.url;
	watcher.body = request.body;

	next((rspCode) => {
		let result = false;
		if (rspCode.status != 200) {
			// rspCode.result = 'noreply';
			// loading = false;
			if (rspCode.status == 401) {
				// rspCode.result = 'noreply';
				// loading = false;
				Midway.alert({
					title: "系统提醒",
					content: "操作员已被签退或失效，请重新登录"
				}).then(function() {
					var _href = location.hostname;
					var _port = location.port || '';
					if(_port){
						_port = ":"+_port
					}
					location.href= 'http://'+_href + _port + '/#!/login'
				})
			}else{	Midway.alert({
					title: "错误信息",
					content: `${rspCode.status || "服务器已断开"} ${rspCode.statusText || "服务已停止"}`
				})}
			return false;
		}


		let data = rspCode.data;
		if (data.hasOwnProperty("rspCode")) {
			if (data.rspCode == "0000") {
				result = true;
			}
			//token失效
			else if (data.rspCode == "5000") {
				let message = {
					title: "系统提醒",
					content: "用户身份认证失效，请重新登录"
				}
				Midway.alert(message).then(function() {
					localStorage.removeItem('id_token');
					window.location.reload();
				})
			}
			//捕获其他错误信息
			else {
				if (data.hasOwnProperty("rspMsg")) {
					if (data.rspMsg) {
						let message = {
							title: "系统提醒",
							content: data.rspMsg || "系统操作失败，请联系管理员"
						}
						Midway.alert(message);
					} else {
						let message = {
							title: "系统提醒",
							content: "异常错误" + data.rspCode + ",请与管理员联系"
						}
						Midway.alert(message);
					}
				}
			}
		}
		rspCode.result = result;
		loading = false;
	});
});

Vue.prototype.$post = function({
	url,
	data ={},
	method = "POST"
}) {
	let json = JSON.parse(localStorage.getItem('id_token'));
	return new Promise((resolve, reject) => {

		this.$http({
			method,
			emulateJSON: true,
			url,
			body: {
                token: json ? json.token : "",
                data:JSON.stringify(data),
                sign:"",
                version:""
			}
		}).then(rspCode => {
            // resolve(rspCode.data)
            rspCode.result ? resolve(rspCode.data) : reject(rspCode.data);
		}, rspCode => {
			if (rspCode) {
				if (rspCode.result == 'noreply') {
					reject()
				} else {
					reject(rspCode.data);
				}
			} else {
				reject()
			}
		})

	});

}

Vue.prototype.$postFormData = function({
	url,
	data,
	method = "POST"
}) {
	let json = JSON.parse(localStorage.getItem('id_token'));
	loading = false

	return new Promise((resolve, reject) => {

		this.$http({
			method,
			emulateJSON: true,
			url,
			body: data
		}).then(rspCode => {
            // resolve(rspCode.data)
			rspCode.result ? resolve(rspCode.data) : reject(rspCode.data);
		}, rspCode => {
			if (rspCode) {
				reject(rspCode.data);
			} else {
				reject()
			}
		})

	});

};

/*商户相关查询*/
Vue.prototype.$search = function({
	url,
	data
}) {
	let json = JSON.parse(localStorage.getItem('id_token'));

	return new Promise((resolve, reject) => {

		this.$http({
			method: "POST",
			emulateJSON: true,
			url,
			body: data
		}).then(rspCode => {
			rspCode.result ? resolve(rspCode.data) : reject(rspCode.data);
		}, rspCode => {
			if (rspCode) {
				if (rspCode.result == 'noreply') {
					reject()
				} else {
					reject(rspCode.data);
				}
			} else {
				reject()
			}
		})

	});

}

/*商户相关操作*/
Vue.prototype.$operate = function({
	url,
	data,
	method = "POST"
}) {

	if (localStorage.getItem('id_token')) {
		let json = JSON.parse(localStorage.getItem('id_token'));
		if (data) {
			data.myToken = json.token;
			data.myName = json.usrName;
			data.myRoleId = json.roleId;
			data.myMchtId = json.mchtId;
			data.mchtIdFlag = localStorage.getItem('get_id')
		} else {
			data = json;
			data.mchtIdFlag = localStorage.getItem('get_id')
		}
	}
	return new Promise((resolve, reject) => {

		this.$http({
			method,
			emulateJSON: true,
			url,
			body: data
		}).then(rspCode => {
			rspCode.result ? resolve(rspCode.data) : reject(rspCode.data);
		}, rspCode => {
			if (rspCode) {
				if (rspCode.result == 'noreply') {
					reject()
				} else {
					reject(rspCode.data);
				}
			} else {
				reject()
			}
		})

	});

}
